export function download(excel) {
    const blob = new Blob([excel], { type: 'text/html' });
    let url = URL.createObjectURL(blob);
    let Doma = document.createElement('a');
    Doma.setAttribute('download', 'index.xlsx');
    Doma.setAttribute('href', url);
    Doma.click();  // 触发事件
    Doma.remove(); // 移除本身 
}

function initBody(tbody) {
    return tbody.map((item, index) => (`<td style="height:50px;color:#fff;background:${item.color};mso-number-format:@">
   ${item.text}
</td>`))
}

export function tableHtmlCompute({ theader, tbody }:{theader:Array<string>,tbody:Array<string>}) {
    let theaders: Array<string> = [];
    let tbodys: Array<string> = [];
    theader.map((item, index) => {
        theaders.push(`
        <td style="height:50px;color:#fff;background:#f16300;mso-number-format:@"><span style="color: rgb(255,0,0)">*</span><span>${item.text}</span></td>
        `)
    })
    tbody.map((item, index) => {
        tbodys.push(`<tr>${initBody(item).join()}</tr>`)
    })
 
    return (
        `<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel'><head><!--[if gte mso 9]><xml>
        <x:ExcelWorkbook>
        <x:ExcelWorksheets>
        <x:ExcelWorksheet>
        <x:WorksheetOptions><x:Print><x:ValidPrinterInfo /></x:Print></x:WorksheetOptions>
        </x:ExcelWorksheet>
        </x:ExcelWorksheets>
        </x:ExcelWorkbook></xml><![endif]--> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>
        <table border="1" align="center" cellspacing="1" cellpadding="1">
        <tr> 
        ${theaders.join()}
        </tr>
        ${tbodys.join()} 
        </table></body></html>`
    )
}